#pragma anon_unions
#include "Ci24R1.h"
#include "nf_module.h"
#include "gpio.h"
#include "spi.h"
//读寄存器
uint8_t CI24R1_Read_Reg(nf_t *nf,uint8_t reg);
//写寄存器
uint8_t CI24R1_Write_Reg(nf_t *nf, uint8_t reg, uint8_t value);
//接收模式
void CI24R1_RX_Mode(nf_t *nf);
//发送模式
void CI24R1_TX_Mode(nf_t *nf);
uint8_t Ci24R1_Check(nf_t *nf);
//发送数据包
uint8_t CI24R1_TxPacket(nf_t *nf,void *txbuf,uint8_t len);
//接收数据包
uint8_t CI24R1_RxPacket(nf_t *nf,void *rxbuf,uint8_t len);

uint8_t CI24R1_Write_command(nf_t *nf,uint8_t command,uint8_t value);

void Ci24R1_IRQ_Manage(nf_t *nf);
void CI24R1_Init(nf_t *nf);
/***********************************
*功能：中断管理，应用层检测中断产生是调用该函数
*参数: 无
*返回：
***********************************/
void Ci24R1_IRQ_Manage(nf_t *nf)
{
	nf->status.irq=1;
	//STATUS_Regist status;
	//status.reg=SI24R1_Read_Reg(nf,STATUS);
	//nf->status.Max_retry=status.MAX_RT;        //达到最大重发次数
	//nf->status.RX_FIFO_IsData=status.RX_DR;    //RX FIFO 有数据标志
	//nf->status.RX_P_NO=status.RX_P_NO;        //接收道数据的管道
	//nf->status.TX_FIFO_Full=status.TX_FULL;   //TX FIFO满
	//nf->status.TX_Complete=status.TX_DS;      //发射完成
	//SI24R1_Write_Reg(nf,STATUS, status.reg);
}
/********************************************************
 * 功能：CI24R1 初始化
 * ******************************************************/
void nf_Setup_Ci24R1_noname_f(nf_t *nf,nf_msg_cb byte_cb,nf_msg_cb gpio_and_delay)
{
	nf->byte_cb=byte_cb;
	nf->gpio_and_delay_cb=gpio_and_delay;
	nf->drive.set_tx_model=CI24R1_TX_Mode;
	nf->drive.set_rx_model=CI24R1_RX_Mode;
	nf->drive.tx_Packet=CI24R1_TxPacket;
	nf->drive.rx_Packet=CI24R1_RxPacket;
	nf->drive.irq=Ci24R1_IRQ_Manage;
	nf->gpio_and_delay_cb(nf,NF_MSG_DATA_MODEL,0,0,0);
  CI24R1_Write_command(nf,SELSPI,0x00);    //切换DATA 引脚为SPI接口
	CI24R1_Init(nf);
	Ci24R1_Check(nf);
    //CI24R1_Write_Reg();
}
void CI24R1_Init(nf_t *nf)
{
	 CI24R1_Write_command(nf,SELSPI,0x00);    //切换DATA 引脚为SPI接口
	 CI24R1_Write_command(nf,FLUSH_RX,0x00);        //清除FIFO寄存器 
	 CI24R1_Write_command(nf,FLUSH_TX,0x00);
}
uint8_t CI24R1_Write_command(nf_t *nf,uint8_t command,uint8_t value)
{
  uint8_t status,res;
	nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,0,0,0);
  nf->byte_cb(nf,NF_MSG_BYTE_Transmit,1,&command,&status);	
	nf->byte_cb(nf,NF_MSG_BYTE_Transmit,1,&value,&res);
	nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,1,0,0);
	return(status);
}
/********************************************************
 * 功能：写寄存器值（单字节）
 * 参数：
 *      reg:寄存器映射地址
 *      value：写入寄存器的值                
 * 返回值：
 *      状态值寄存器的值
*********************************************************/
uint8_t CI24R1_Write_Reg(nf_t *nf, uint8_t reg, uint8_t value)
{
	uint8_t status,res;
	reg=reg+NF_WRITE_REG;
	nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,0,0,0);
  nf->byte_cb(nf,NF_MSG_BYTE_Transmit,1,&reg,&status);	
	nf->byte_cb(nf,NF_MSG_BYTE_Transmit,1,&value,&res);
	nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,1,0,0);
	return(status);
}
/********************************************************
 * 功能：写寄存器值（多字节）                
 * 参数：
 *       reg:寄存器地址
 *       pBuf：写入寄存器的数据指针
 *       bytes：写入数据的长度
 * 返回：
 *       状态寄存器的值
*********************************************************/
uint8_t CI24R1_Write_Buf(nf_t *nf,uint8_t reg, uint8_t *pBuf, uint8_t bytes)
{
	uint8_t status;
	uint8_t res[bytes];
	reg=reg+NF_WRITE_REG;
  nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,0,0,0);
  nf->byte_cb(nf,NF_MSG_BYTE_Transmit,1,&reg,&status);
  nf->byte_cb(nf,NF_MSG_BYTE_Transmit,bytes,pBuf,res);
  nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,1,0,0);                                     	
  return(status);       
}		
uint8_t CI24R1_Write_Data(nf_t *nf,uint8_t reg, uint8_t *pBuf, uint8_t bytes)
{
	uint8_t status;
	uint8_t res[bytes];
	reg=reg;
  nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,0,0,0);
  nf->byte_cb(nf,NF_MSG_BYTE_Transmit,1,&reg,&status);
  nf->byte_cb(nf,NF_MSG_BYTE_Transmit,bytes,pBuf,res);
  nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,1,0,0);                                     	
  return(status);       
}
/********************************************************
 * 功能：读寄存器的值（单字节）
 * 参数：
 *       reg:寄存器地址（格式：READ_REG|reg）
 * 返回：
 *       寄存器值
*********************************************************/
uint8_t CI24R1_Read_Reg(nf_t *nf,uint8_t reg)
{
  uint8_t value,res;
	reg=reg+NF_READ_REG;
	nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,0,0,0);    //CS低
  nf->byte_cb(nf,NF_MSG_BYTE_Transmit,1,&reg,&res);
  nf->byte_cb(nf,NF_MSG_BYTE_Receive,1,&res,&value);
	nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,1,0,0);    //CS 高            
	return(res);
}


/********************************************************
 * 功能：读寄存器的值（多字节）
 * 参数：
 *       reg:寄存器地址
 *       pBuf:接收数据指针
 *       bytes:接收数据的长度
 * 返回：
 *       状态寄存器的值
*********************************************************/
uint8_t CI24R1_Read_Buf(nf_t *nf,uint8_t reg, uint8_t *pBuf, uint8_t bytes)
{
	uint8_t status;
	uint8_t res[bytes];
	reg=reg+NF_READ_REG;
  nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,0,0,0);    //CS低
  nf->byte_cb(nf,NF_MSG_BYTE_Transmit,1,&reg,&status);
	nf->byte_cb(nf,NF_MSG_BYTE_Receive,bytes,pBuf,res);
  nf->gpio_and_delay_cb(nf,NF_MSG_GPIO_CS,1,0,0);    //CS 高  	
  return(status);    
}
/*******************************
 *
*******************************/
uint8_t Ci24R1_Check(nf_t *nf)
{
	uint8_t i;
	uint8_t buf[5]={0xA5,0xA5,0xA5,0xA5,0xA5};
  uint8_t buf2[5]={0};  
  CI24R1_Write_command(nf,CE_OFF,0x00);
	CI24R1_Write_Buf(nf,TX_ADDR,(uint8_t *)buf,5);
	CI24R1_Read_Buf(nf,TX_ADDR,(uint8_t *)buf2,5);
	for(i=0;i<5;i++)
	  if(buf2[i]!=0xA5)
			break;
	if(i!=5)
		return 1;  			//¼ì²âCi24R1´íÎó
	return 0;				//¼ì²âµ½Ci24R1
}
/********************************************************
 * 功能：SI24R1 接收模式初始化
 * 参数：无
 * 返回：无
*********************************************************/
void CI24R1_RX_Mode(nf_t *nf)
{
	uint8_t res_add[5]={0};
	EN_RXADDR_Regist en_rx_channel_reg;                //通道使能寄存器
	ENAA_Regist      enaa_reg;                         //通道使能自动确认寄存器
	Config_Regist    config_reg;                       //配置寄存器
  RF_SETUP_Regist     rf_setup_regist;              //射频配置寄存器
  CI24R1_Write_command(nf,CE_OFF,0x00);	
	en_rx_channel_reg.reg=0x00;
	enaa_reg.reg=0x00;
	config_reg.reg=0x00;
	rf_setup_regist.reg=0;
    //接收设备接收通道0使用和发送设备相同的地址
	CI24R1_Write_Buf(nf,RX_ADDR_P0, nf->nf_config.rx_config.rx_add.rx_addr_p0, nf->nf_config.rx_config.rx_add.add_width);
	
	  //使能接收通道0
	en_rx_channel_reg.ERX_P0=nf->nf_config.rx_config.p0.EN_Rxddr;
	en_rx_channel_reg.ERX_P1=nf->nf_config.rx_config.p1.EN_Rxddr;
	en_rx_channel_reg.ERX_P2=nf->nf_config.rx_config.p2.EN_Rxddr;
	en_rx_channel_reg.ERX_P3=nf->nf_config.rx_config.p3.EN_Rxddr;
	en_rx_channel_reg.ERX_P4=nf->nf_config.rx_config.p4.EN_Rxddr;
	en_rx_channel_reg.ERX_P5=nf->nf_config.rx_config.p5.EN_Rxddr;
	CI24R1_Write_Reg(nf,EN_RXADDR, en_rx_channel_reg.reg);
	
	
	 //使能接收通道0自动应答
	enaa_reg.ENAA_P0=nf->nf_config.rx_config.p0.ENAA;
	enaa_reg.ENAA_P1=nf->nf_config.rx_config.p1.ENAA;
	enaa_reg.ENAA_P2=nf->nf_config.rx_config.p2.ENAA;
	enaa_reg.ENAA_P3=nf->nf_config.rx_config.p3.ENAA;
	enaa_reg.ENAA_P4=nf->nf_config.rx_config.p4.ENAA;
	enaa_reg.ENAA_P5=nf->nf_config.rx_config.p5.ENAA;
	CI24R1_Write_Reg(nf,EN_AA, enaa_reg.reg);
	
	
    //选择射频通道
	CI24R1_Write_Reg(nf,RF_CH, nf->nf_config.rf_ch);  
	
    //接收通道选择和发送通道相同有效数据宽度
	CI24R1_Write_Reg(nf,RX_PW_P0, nf->nf_config.rx_config.p0.Data_witdh);
	CI24R1_Write_Reg(nf,RX_PW_P1, nf->nf_config.rx_config.p1.Data_witdh);
	CI24R1_Write_Reg(nf,RX_PW_P2, nf->nf_config.rx_config.p2.Data_witdh);
	CI24R1_Write_Reg(nf,RX_PW_P3, nf->nf_config.rx_config.p3.Data_witdh);
	CI24R1_Write_Reg(nf,RX_PW_P4, nf->nf_config.rx_config.p4.Data_witdh);
	CI24R1_Write_Reg(nf,RX_PW_P5, nf->nf_config.rx_config.p5.Data_witdh);
	
	rf_setup_regist.RF_DR=nf->nf_config.rf_dr;     //速率
	rf_setup_regist.RF_PWR=nf->nf_config.rf_pwr;   //发射功率
    //数据传输速度2Mbps，发送功率7dbm
	CI24R1_Write_Reg(nf,RF_SETUP, rf_setup_regist.reg);
	
	//CRC使能，16位CRC校验，上电，接收模式
	config_reg.EN_CRC=nf->nf_config.crc_config.en_crc;
	config_reg.CRCO=nf->nf_config.crc_config.crco;
	config_reg.PWR_UP=1;
	config_reg.PRIM_RX=1;
	config_reg.MASK_MAX_RT=0;
	config_reg.MASK_RX_DR=0;
	config_reg.MASK_TX_DS=0;
	nf->status.PRIM_RX = config_reg.PRIM_RX;
	CI24R1_Write_Reg(nf,CONFIG, config_reg.reg);
	CI24R1_Write_command(nf,FLUSH_RX,0x00);
    //拉高CE驱动接收设备
	CI24R1_Write_command(nf,CE_ON,0x00);
}	

/********************************************************
 * 功能：SI24R1发送模式初始化
 * 参数：无
 * 返回：无
*********************************************************/
void CI24R1_TX_Mode(nf_t *nf)
{
	uint8_t res=0;
	SETUP_RETR_Regist   setup_retr_reg;
	Config_Regist       config_reg;                 //配置寄存器
	RF_SETUP_Regist     rf_setup_regist;            //射频配置寄存器
	setup_retr_reg.reg=0x00;
	rf_setup_regist.reg=0x00;
	CI24R1_Write_command(nf,CE_OFF,0x00);	
    //写入发送地址
	CI24R1_Write_Buf(nf,TX_ADDR, nf->nf_config.tx_config.tx_add, nf->nf_config.tx_config.address_witdh);
    //为了应答接收设备，接收通道0地址和发送地址相同
	CI24R1_Write_Buf(nf,RX_ADDR_P0, nf->nf_config.tx_config.tx_add, nf->nf_config.tx_config.address_witdh);
    //使能接收通道0自动应答
	CI24R1_Write_Reg(nf,EN_AA, 0x01);
	//res=SI24R1_Read_Reg(nf,EN_AA);
    //使能接收通道0
	CI24R1_Write_Reg(nf,EN_RXADDR, 0x01);
	
  //自动重发延时等待250us+86us，自动重发10次
	setup_retr_reg.ARC=nf->nf_config.tx_config.retr_arc;
	setup_retr_reg.ARD=nf->nf_config.tx_config.retr_ard;
	CI24R1_Write_Reg(nf,SETUP_RETR, setup_retr_reg.reg);
	
  //选择射频通道
	CI24R1_Write_Reg(nf,RF_CH, nf->nf_config.rf_ch);
	
  //数据传输速率2Mbps，发射功率7dBm
	rf_setup_regist.RF_DR=nf->nf_config.rf_dr;     //速率
	rf_setup_regist.RF_PWR=nf->nf_config.rf_pwr;   //发射功率
	CI24R1_Write_Reg(nf,RF_SETUP, rf_setup_regist.reg);
	
  //CRC使能，16位CRC校验，上电
	config_reg.EN_CRC=nf->nf_config.crc_config.en_crc;   //CRC 使能配置
	config_reg.CRCO=nf->nf_config.crc_config.crco;       //CRC 校验字节长度
	config_reg.PWR_UP=1;                      //开机模式
	config_reg.PRIM_RX=0;                     //发射模式
	config_reg.MASK_TX_DS=0;
	config_reg.MASK_MAX_RT=0;
	config_reg.MASK_RX_DR=0;
	nf->status.PRIM_RX=0;
	nf->status.pwr_up=1;
	nf->status.PRIM_RX = config_reg.PRIM_RX;
	CI24R1_Write_Reg(nf,CONFIG, config_reg.reg);
	CI24R1_Write_command(nf,FLUSH_TX,0x00);
	CI24R1_Write_command(nf,CE_ON,0x00);	
}


/********************************************************
 * 功能：读接收的数据
 * 参数：
 *       *rxbuf：接收缓存区指针
 * 返回：
 *       接收状态 0：接收到数据 1：没有接收到数据
*********************************************************/
uint8_t CI24R1_RxPacket(nf_t *nf,void *rxbuf,uint8_t len)
{
	uint8_t i=0;
	STATUS_Regist status_reg;
	CI24R1_Write_command(nf,SELIRQ,0x00);
	nf->gpio_and_delay_cb(nf,NF_MSG_IRQ_MODEL,0,0,0);
	while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==GPIO_PIN_SET)
	{
		nf->gpio_and_delay_cb(nf,NF_MSG_DELAY_MILLI,10,0,0);
		i++;
		if(i>10)
		{
			break;
		}
	};
	nf->gpio_and_delay_cb(nf,NF_MSG_DATA_MODEL,0,0,0);
	CI24R1_Write_command(nf,SELSPI,0x00);
	status_reg.reg = CI24R1_Read_Reg(nf,STATUS);  			                //读取状态寄存器的值    	  
	CI24R1_Write_Reg(nf,STATUS,status_reg.reg);                             //清除RX_DS中断标志
	if(status_reg.RX_DR)								                    //接收到数据
	{
		//CI24R1_Read_Buf(nf,RD_RX_PLOAD,rxbuf,len);         //读取数据
		while(status_reg.RX_P_NO!=0x07)
		{
			CI24R1_Read_Buf(nf,RD_RX_PLOAD,rxbuf,len);         //读取数据
			status_reg.reg = CI24R1_Read_Reg(nf,STATUS);
		}
		CI24R1_Write_command(nf,FLUSH_RX,0x00);				                    //清除RX FIFO寄存器
		return 0; 
	}
	return 1;                                                               //没有收到任何数据
}
/********************************************************
 * 功能：发送一个数据包
 * 参数：
 *       txbuf：发送数据的指针   
*********************************************************/
uint8_t CI24R1_TxPacket(nf_t *nf,void *txbuf,uint8_t len)
{
	uint8_t i=0;
	uint8_t state;
	STATUS_Regist status_reg={0};
	nf->status.irq=0;
  //CE拉低，使能SI24R1配置
	CI24R1_Write_command(nf,CE_OFF,0x00);
  CI24R1_Write_command(nf,FLUSH_TX,0x00);		
  //写数据到TX FIFO,32个字节
  CI24R1_Write_Data(nf,WR_TX_PLOAD,txbuf,len);
 	CI24R1_Write_command(nf,CE_ON,0x00);	
	
	CI24R1_Write_command(nf,SELIRQ,0x00);
	nf->gpio_and_delay_cb(nf,NF_MSG_IRQ_MODEL,0,0,0);
	while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==GPIO_PIN_SET)
	{
		nf->gpio_and_delay_cb(nf,NF_MSG_DELAY_MILLI,10,0,0);
		i++;
		if(i>100)
		{
			break;
		}
	};
	nf->gpio_and_delay_cb(nf,NF_MSG_DATA_MODEL,0,0,0);
	CI24R1_Write_command(nf,SELSPI,0x00);

  status_reg.reg= CI24R1_Read_Reg(nf,STATUS);            //读取状态寄存器的值 
 
	CI24R1_Write_Reg(nf,STATUS,  status_reg.reg);  //清除TX_DS或MAX_RT中断标志
	if(status_reg.TX_DS)
	{
		return 0x01;
	}
	if(status_reg.MAX_RT)                            //达到最大重发次数
	{		
		CI24R1_Write_command(nf,FLUSH_TX,0x00);
		return 0x00; 
	}
	return 0XFF;		                             //发送失败
}
uint8_t ReadRSSI(nf_t *nf)
{
	return CI24R1_Read_Reg(nf,RSSI);
}
